package com.open.video.service.impl;

import com.open.capacity.common.model.SysCompany;
import com.open.capacity.common.model.SysRole;
import com.open.capacity.common.model.SysUser;
import com.open.video.dao.VUserRoleDao;
import com.open.video.dao.VideoDao;
import com.open.video.model.VideoModel;
import com.open.video.service.VideoService;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class VideoServiceImpl implements VideoService {

    @Resource
    VideoDao videoDao;
    @Resource
    VUserRoleDao vUserRoleDao;
    /**
     * 视屏上传成功保存在数据库
     * @param videoModel1
     * @return
     */
    @Override
    public int addVideo(VideoModel videoModel1) {
        int result= videoDao.addVideo(videoModel1);
        return result;
    }

    /**
     * 删除aliyun以及数据库中的数据
     * @param vid
     * @return
     */
    @Override
    public int deletesVideo(String vid) {
        int result=videoDao.deletesVideo(vid);
        return result;
    }

    /**
     * 展示所有上传的视屏的地址信息
     * @return
     */
    @Override
    public List<VideoModel> selectAllVideo(Map<String,Object> params) {
        List<VideoModel>list=videoDao.selectAllVideo(params);
        return list;
    }

    /**
     * 根据id查找视频信息
     * @param id
     * @return
     */
    @Override
    public VideoModel selectVideoById(Integer id) {
        VideoModel videoModel=videoDao.selectVideoById(id);
        return videoModel;
    }

    /**
     * 根据字段名模糊查找视频
     * @param params
     * @return
     */
    @Override
    public List<VideoModel> findSomeVideo( Map<String,Object> params) {

        Object userId1 = params.get("userId");
        long userId = 0;
        boolean isAdmin=false; //默认不是管理员
        if(userId1!=null){
            userId = Long.valueOf(params.get("userId").toString());
            Set<SysRole> rolesByUserId = vUserRoleDao.findRolesByUserId(userId);
            Iterator<SysRole> iterator = rolesByUserId.iterator();
            while (iterator.hasNext()){
                SysRole next = iterator.next();
                if(next.getCode().equals("ADMIN")){
                    isAdmin=true;
                    break;
                }
            }
        }

        if(isAdmin) {
            params.remove("userId");
        }else {
            SysUser user = vUserRoleDao.findUserById(userId);
            Long companyId = user.getCompanyId();
            params.remove("userId");
            params.put("companyId",companyId);
        }
        List<VideoModel>list=videoDao.findSomeVideo(params);
        int page =Integer.parseInt(params.get("page").toString())-1;
        params.put("pages",page);
        return list;
    }

    /**
     * 发布视屏按照降序排序
     * @return
     */
    @Override
    public List<VideoModel> DescVideo() {
        List<VideoModel>list=videoDao.DescVideo();
        return list;
    }

    /**
     * 视屏修改
     * @param videoModel
     * @return
     */
    @Override
    public int updateVideo(VideoModel videoModel) {
        return videoDao.updateVideo(videoModel);
    }

    /**
     * 查询公司
     * @return
     */
    @Override
    public List<SysCompany> selectCompany() {
        return videoDao.selectCompany();
    }
}
